package com.ihisomp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ihisomp.base.Page;
import com.ihisomp.base.Result;
import com.ihisomp.dto.SysAntibioticDto;
import com.ihisomp.entity.SysAntibiotic;
import com.ihisomp.mapper.SysAntibioticMapper;
import com.ihisomp.service.ISysAntibioticService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * 标题: SysAntibioticServiceImpl.java
 * 业务描述: 抗生素等级字典 服务实现类
 * 作者: zy
 * 日期: 2021/9/13
 * 版本: V1.0
 */
@Service
public class SysAntibioticServiceImpl extends ServiceImpl<SysAntibioticMapper, SysAntibiotic> implements ISysAntibioticService {

    /**
     * 新增数据
     *
     * @param sysAntibioticDto
     * @return
     */
    @Override
    public Result add(SysAntibioticDto sysAntibioticDto) {
        if (sysAntibioticDto == null || StringUtils.isBlank(sysAntibioticDto.getAntibioticKey())) {
            return Result.error("抗生素等级编码不能为空！");
        }
        if (StringUtils.isBlank(sysAntibioticDto.getAntibioticName())) {
            return Result.error("抗生素等级名称不能为空！");
        }
        if (baseMapper.selectById(sysAntibioticDto.getAntibioticKey()) != null) {
            return Result.error("抗生素等级已经存在！");
        }
        SysAntibiotic sysAntibiotic = new SysAntibiotic();
        BeanUtils.copyProperties(sysAntibioticDto, sysAntibiotic);
        if (baseMapper.insert(sysAntibiotic) > 0) {
            return Result.ok();
        }
        return Result.error("新增抗生素等级失败！");
    }

    /**
     * 删除数据
     *
     * @param antibioticKey
     * @return
     */
    @Override
    public Result delete(String antibioticKey) {
        SysAntibiotic sysAntibiotic = baseMapper.selectById(antibioticKey);
        if (sysAntibiotic == null) {
            return Result.error("无查询抗生素等级信息！");
        }
        if (baseMapper.deleteById(antibioticKey) > 0) {
            return Result.ok();
        }
        return Result.error("删除抗生素等级失败！");
    }

    /**
     * 更新数据
     *
     * @param antibioticKey
     * @param sysAntibioticDto
     * @return
     */
    @Override
    public Result update(String antibioticKey, SysAntibioticDto sysAntibioticDto) {
        SysAntibiotic sysAntibiotic = new SysAntibiotic();
        BeanUtils.copyProperties(sysAntibioticDto, sysAntibiotic);
        if (sysAntibiotic.getAntibioticKey() == null) {
            sysAntibiotic.setAntibioticKey(antibioticKey);
        }
        if (baseMapper.updateById(sysAntibiotic) > 0) {
            return Result.ok();
        } else {
            return Result.error("更新抗生素等级失败！");
        }
    }

    /**
     * 查询数据
     *
     * @param antibioticKey
     * @return
     */
    @Override
    public Result get(String antibioticKey) {
        SysAntibiotic sysAntibiotic = baseMapper.selectById(antibioticKey);
        if (sysAntibiotic == null) {
            return Result.error("无查询抗生素等级信息！");
        }
        return Result.ok(sysAntibiotic);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @param sysAntibioticDto
     * @return
     */
    @Override
    public Result search(long page, long size, SysAntibioticDto sysAntibioticDto) {
        //  查询条件
        QueryWrapper<SysAntibiotic> query = new QueryWrapper<>();
        if (sysAntibioticDto != null) {
            if (StringUtils.isNotBlank(sysAntibioticDto.getAntibioticName())) {
                query.like("antibiotic_name", sysAntibioticDto.getAntibioticName());
            }
        }
        // 分页查询
        IPage<SysAntibiotic> data = baseMapper.selectPage(new Page<>(page, size), query);
        return Result.ok(data);
    }
}
